import WxValidate from "../../../../utils/wx-validate/WxValidate";
import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    iconSize: [20],
    iconColor: ['#f00'],
    iconType: ['cancel'],
    token:null,
    form: {
      id:"",
      bankname:"",
      card_name:"",
      limit:"",
      bill_day:"",
      repay_day: "",
      annual_fee_day: ""
    },
    annualFeeIndex:[0,0],
    billDateInfo: [],
    repayDateInfo: [],
    annualFeeInfo: [],
    formUrl:"/API/Card/addCard"
  },
  onLoad: async function() {
    this.initValidate();
    this.initBaseData();
    const token = await app.getToken();
    this.setData({
      token:token
    })
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      bankname: {
        required: true
      },
      card_name: {
        required: true
      },
      limit: {
        required: true
      },
      bill_day: {
        required: true
      },
      repay_day: {
        required: true
      },
      annual_fee_day: {
        required: true
      }
    };

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      bankname: {
        required: "请选择银行"
      },
      card_name: {
        required: "请填写卡片名称"
      },
      limit: {
        required: "请输入额度"
      },
      bill_day: {
        required: "请选择账单日"
      },
      repay_day: {
        required: "请选择还款日"
      },
      annual_fee_day: {
        required: "请选择年费日"
      }
    };

    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages);
  },
  initBaseData:function(){
    var arr=[],annualFeeInfo=[[],[]];
    for(var i=1;i<=28;i++){
      arr.push({"name":i+"号",id:i});
      annualFeeInfo[1].push({"name":i+"日",id:i})
    }
    for(var i=1;i<=12;i++){
      annualFeeInfo[0].push({"name":i+"月",id:i})
    }
    this.setData({
      billDateInfo:arr.slice(0),
      repayDateInfo:arr.slice(0),
      annualFeeInfo:annualFeeInfo
    })
  },
  pickerColumnChange:function(e){
    console.log("pickerColumnChange");
    console.log(e);
    var value=e.detail.value+1;
    var column=e.detail.column;
    if(column==0){
      var max;
      switch(true){
        case([1,3,5,7,8,10,12].indexOf(value)>-1):
          max=31;
          break;
        case([2].indexOf(value)>-1):
          max=28;
          break;
        default:
          max=30;
          break;
      }
      var arr=[];
      for(var i=1;i<=max;i++){
        arr.push({"name":i+"日",id:i})
      }
      this.setData({
        "annualFeeInfo[1]":arr
      })
    }
  },
  pickerMultiChange:function(e){
    console.log("pickerMultiChange");
    console.log(e);
    var value=e.detail.value;
    var annualFeeInfo=this.data.annualFeeInfo;
    var annual_fee_day=annualFeeInfo[0][value[0]].id+"/"+annualFeeInfo[1][value[1]].id;
    this.setData({
      annualFeeIndex:value,
      'form.annual_fee_day':annual_fee_day
    })
    console.log(annual_fee_day);
  },
  pickerCancel:function(e){
    console.log("pickerCancel");
    console.log(this.data.annualFeeIndex);
    /* this.setData({
      annualFeeIndex:this.data.annualFeeIndex
    }) */
  },
  submitForm(e) {
    const params = e.detail.value;
    params.formId = e.detail.formId;
    console.log(params);
    // 传入表单数据，调用验证方法
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      this.showModal(error);
      return false;
    }
    wx.showLoading({
      title:"提交中，请稍后..."
    })
    //debugger;
    params.token=this.data.token;
    params.bank_id=22; //临时添加 bank_id
    console.log(params);
    app.request(this.data.formUrl,params,res=>{
      wx.hideLoading();
      var data=res.data;
      if(data.state){
        this.showModal(data,function(){
          wx.navigateBack({
            delta: 1
          })
        })
      }
      else{
        this.showModal(data);
      }
    },()=>{
      wx.hideLoading();
      this.showModal('提交失败，请重试');
    })
  },
  showModal(error,cb) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
      complete: function(){
        if(cb) cb();
      }
    });
  },
  pickerChange(e) {
    var name = "form." + e.target.dataset.name;
    var arr = this.data[e.target.dataset.arr];
    this.setData({
      [name]: arr[e.detail.value].id
    });
  }
});
